package com.supermarket.service;

import com.supermarket.entity.OrderPayment;
import com.supermarket.mapper.OrderPaymentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 订单支付信息Service实现类
 */
@Service
public class OrderPaymentService {

    @Autowired
    private OrderPaymentMapper orderPaymentMapper;

    /**
     * 创建支付信息
     * @param orderPayment 支付信息
     * @return 是否成功
     */
    public boolean createOrderPayment(OrderPayment orderPayment) {
        orderPayment.setPaymentStatus("PENDING"); // 默认待支付状态
        orderPayment.setCreateTime(new Date());
        orderPayment.setUpdateTime(new Date());
        return orderPaymentMapper.insertOrderPayment(orderPayment) > 0;
    }
    
    /**
     * 根据ID查询支付信息
     * @param id 支付ID
     * @return 支付信息
     */
    public OrderPayment getOrderPaymentById(Integer id) {
        return orderPaymentMapper.selectOrderPaymentById(id);
    }
    
    /**
     * 根据订单ID查询支付信息
     * @param orderId 订单ID
     * @return 支付信息
     */
    public OrderPayment getOrderPaymentByOrderId(Integer orderId) {
        return orderPaymentMapper.selectOrderPaymentByOrderId(orderId);
    }
    
    /**
     * 根据支付流水号查询支付信息
     * @param paymentNo 支付流水号
     * @return 支付信息
     */
    public OrderPayment getOrderPaymentByPaymentNo(String paymentNo) {
        return orderPaymentMapper.selectOrderPaymentByPaymentNo(paymentNo);
    }
    
    /**
     * 更新支付状态为成功
     * @param id 支付ID
     * @param paymentNo 支付流水号
     * @return 是否成功
     */
    @Transactional
    public boolean updatePaymentToSuccess(Integer id, String paymentNo) {
        Date now = new Date();
        OrderPayment payment = orderPaymentMapper.selectOrderPaymentById(id);
        
        // 验证支付单状态
        if (payment != null && "PENDING".equals(payment.getPaymentStatus())) {
            // 更新支付流水号
            int result = orderPaymentMapper.updatePaymentNo(id, paymentNo);
            
            if (result > 0) {
                // 更新支付时间
                orderPaymentMapper.updatePaymentTime(id, now);
                
                // 更新支付状态为成功
                orderPaymentMapper.updatePaymentStatus(id, "SUCCESS", now);
                
                return true;
            }
        }
        return false;
    }
    
    /**
     * 更新支付状态为失败
     * @param id 支付ID
     * @param paymentNo 支付流水号（可为空）
     * @return 是否成功
     */
    @Transactional
    public boolean updatePaymentToFailed(Integer id, String paymentNo) {
        Date now = new Date();
        OrderPayment payment = orderPaymentMapper.selectOrderPaymentById(id);
        
        // 验证支付单状态
        if (payment != null && "PENDING".equals(payment.getPaymentStatus())) {
            // 如果有支付流水号，更新支付流水号
            if (paymentNo != null && !paymentNo.isEmpty()) {
                orderPaymentMapper.updatePaymentNo(id, paymentNo);
            }
            
            // 更新支付状态为失败
            return orderPaymentMapper.updatePaymentStatus(id, "FAILED", now) > 0;
        }
        return false;
    }
    
    /**
     * 更新支付状态为退款
     * @param id 支付ID
     * @return 是否成功
     */
    @Transactional
    public boolean updatePaymentToRefund(Integer id) {
        Date now = new Date();
        OrderPayment payment = orderPaymentMapper.selectOrderPaymentById(id);
        
        // 验证支付单状态
        if (payment != null && "SUCCESS".equals(payment.getPaymentStatus())) {
            // 更新支付状态为退款
            return orderPaymentMapper.updatePaymentStatus(id, "REFUND", now) > 0;
        }
        return false;
    }
    
    /**
     * 根据支付状态查询支付信息列表
     * @param paymentStatus 支付状态
     * @return 支付信息列表
     */
    public List<OrderPayment> getOrderPaymentsByStatus(String paymentStatus) {
        return orderPaymentMapper.selectOrderPaymentsByStatus(paymentStatus);
    }
    
    /**
     * 根据时间范围查询支付信息
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 支付信息列表
     */
    public List<OrderPayment> getOrderPaymentsByTimeRange(Date startTime, Date endTime) {
        return orderPaymentMapper.selectOrderPaymentsByTimeRange(startTime, endTime);
    }
    
    /**
     * 查询特定支付平台的支付信息
     * @param paymentPlatform 支付平台
     * @return 支付信息列表
     */
    public List<OrderPayment> getOrderPaymentsByPlatform(String paymentPlatform) {
        return orderPaymentMapper.selectOrderPaymentsByPlatform(paymentPlatform);
    }
    
    /**
     * 更新支付信息
     * @param orderPayment 支付信息
     * @return 是否成功
     */
    public boolean updateOrderPayment(OrderPayment orderPayment) {
        orderPayment.setUpdateTime(new Date());
        return orderPaymentMapper.updateOrderPayment(orderPayment) > 0;
    }
} 